<?php
declare (strict_types=1);

namespace app\admin\controller;


use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use app\common\tool\Api;
use app\common\tool\Redis;
use think\App;

class Logs extends Base
{

    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Logs';

    //关联模型
    protected $hasOne = [];

    public function index()
    {
        return $this->setTable();
    }

    public function setTable()
    {
        //构建table
        $table = [
            ['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80],
            ['field' => 'emp_name', 'title' => '操作人'],
            ['title' => '操作IP', 'templet' => '#logTpl'],
            ['field' => 'event', 'title' => '操作方法'],
            ['field' => 'description', 'title' => '描述'],
            ['field' => 'create_time', 'title' => '时间', 'fixed' => 'right', 'align' => 'center'],
        ];

        return TableBuilder::getInstance()
            ->addColumns(TableFront::getInstance()->addBatchRows($table))
            ->setSearch($this->setSearch())
            ->addJsUrl(['logs'])
            ->addExtraHtml(['logs'])
            ->fetch();
    }

    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['emp_name__ip', '会员编号/管理员账号/IP地址查询', 'input'])
            ->setSearch(['description', '输入关键字筛选', 'input', '', '', '', 'like']);
    }


    public function queryIp($ip)
    {
        if ($ip == '127.0.0.1') {
            Redis::Instance()->hSet('ipAddress', '127.0.0.1', '本地IP');
            return json(['code' => 1, 'msg' => '查询成功', 'result' => '本地IP']);
        }

        $res = queryIp($ip);
        if ($res) {
            Redis::Instance()->hSet('ipAddress', $ip, $res);
            return json(['code' => 1, 'msg' => '查询成功', 'result' => $res]);
        } else {
            return json(['code' => -1, 'msg' => '查询失败']);
        }
    }

}
